package Entity;

import java.util.Arrays;
import java.util.Date;

import Controller.DBcontroller;

/**
 * This is one of the reports.
 * this report is for malfunctions and exceptions, and it will be produced to the company manager.
 * @author Shahar Shelli
 *
 */
public class MalfuncExceptReport extends Report
{
	Order[][] lateness;
	int []numOfCancels;
	Date start;
	
	/**
	 * is the constructor. it will call the super constructor 
	 * and also set the number of lateness and number of canceled parking spots.
	 * this will be a report for a specific parking lot.
	 * @param s= the start date.
	 * @param e= the end date. e and s will determine the period of time.
	 * @param des= the description for the report.(optional that's why we need this constructor)
	 */
	public MalfuncExceptReport(Date s,Date e,String des) 
	{
		super(e,des);
		this.start=s;
		for(int i=0; i<6; i++)
		{
			this.lateness[i]=DBcontroller.getLateness(i);
			this.numOfCancels[i]=DBcontroller.getNumOfCanceled(i);
		}
		
	}
	
	/**
	 * is the constructor. it will call the super constructor 
	 * and also set the number of lateness and number of canceled parking spots.
	 * this will be a report for a specific parking lot.
	 * @param s= the start date.
	 * @param e= the end date. e and s will determine the period of time.
	 */
	public MalfuncExceptReport(Date s,Date e) 
	{
		super(e,"");
		this.start=s;
		for(int i=0; i<6; i++)
		{
			this.lateness[i]=DBcontroller.getLateness(i);
			this.numOfCancels[i]=DBcontroller.getNumOfCanceled(i);
		}
		
	}
	
	// the get functions.
	public Date getStart() {
		return start;
	}
	
	private Date[] getLengthOfLate(int parkinglot)
	{
		Date[] arr=new Date[this.lateness[parkinglot-1].length];
		for(int i=0; i<arr.length; i++)
			arr[i]=new Date(this.lateness[parkinglot-1][0].getActualArrivalDate().getTime()-this.lateness[parkinglot-1][0].getArrivalDate().getTime());
		return arr;
	}
	
	public double NumOfLateHours(int parkinglot)
	{
		Date[] late=this.getLengthOfLate(parkinglot+1);
		double hour=0,minutes=0,second=0;
		for(int i=0; i<late.length; i++)
		{
			hour+=late[i].getHours();
			minutes+=late[i].getMinutes();
			second+=late[i].getSeconds();
		}
		minutes+=(second/60);
		hour+=(minutes/60);
		double sum=hour+(minutes/60);
		return sum;
	}
	
	public double NumOfLateHoursMed()
	{
		double [] arr=new double[this.numOfCancels.length];
		for(int i=0; i<arr.length; i++)
			arr[i]=this.NumOfLateHours(i+1);
		Arrays.sort(arr);
		return arr[arr.length/2];
	}
	
	public Date getLengthOfLateMedForLot(int parkinglot)
	{
		Date[] late=this.getLengthOfLate(parkinglot);
		Arrays.sort(late);
		return late[late.length/2];
	}
	
	public Date getLengthOfLateMed()
	{
		Date[] late=new Date[this.numOfCancels.length];
		for(int i=0; i<late.length; i++)
			late[i]=new Date(this.getLengthOfLateMedForLot(i+1).getTime());
		Arrays.sort(late);
		return late[late.length/2];
	}
	
	public int getCanceled(int p)
	{
		return this.numOfCancels[p-1];
	}
	
	public int getLateness(int p)
	{
		return this.lateness[p-1].length;
	}
	
	public int getLatenessMedian()
	{
		int [] arr=new int[this.numOfCancels.length];
		for(int i=0; i<arr.length; i++)
			arr[i]=this.getLateness(i+1);
		Arrays.sort(arr);
		return arr[arr.length/2];
	}
	
	public int getMedianCancel()
	{
		Arrays.sort(this.numOfCancels);
		return this.numOfCancels[this.numOfCancels.length/2];
	}
	
	public int[] getCancelFrequencyDist()
	{
		Arrays.sort(this.numOfCancels);
		int[]arr=new int[this.numOfCancels[this.numOfCancels.length-1]]; for(int k=0; k<arr.length; k++) arr[k]=0;
		for(int i=0; i<this.numOfCancels.length; i++)
			arr[this.numOfCancels[i]-1]++;
		return arr;
	}
	
	public int[] getLatenessFrequencyDist()
	{
		int [] arr=new int[this.numOfCancels.length];
		for(int i=0; i<arr.length; i++) arr[i]=this.getLateness(i+1);
		Arrays.sort(arr);
		int[]arr1=new int[arr[arr.length-1]]; for(int k=0; k<arr1.length; k++) arr1[k]=0;
		for(int i=0; i<this.numOfCancels.length; i++)
			arr1[this.numOfCancels[i]-1]++;
		return arr1;
	}
	
	
}
